set :mongrel_ports, 3001..3003
set :deploy_to, "/usr/rails"

namespace :railsyard do
  def gem_install_direct(uri)
    gem = uri.sub(/^.*\//,"")
    p [gem, uri]
    run "wget -O /tmp/#{gem} #{uri}"
    run "gem install /tmp/#{gem}"
  end
  def gem_install(packages)
    run "gem install -r -y "+packages.to_a.join(" ")
  end
  def apt_install(packages)
    run "apt-get -q -y install "+packages.to_a.join(" ")
  end

  task :setup do
    apt_install "sudo subversion"
    setup_app
    setup_web
  end
  task :setup_app, :rols=>:app do
    run "apt-get -q update"
    apt_install "ruby1.8 ruby1.8-dev irb rdoc ri rubygems gcc libc6-dev make"
    setup_mongrel
    setup_monit
  end

  task :setup_mongrel, :roles=>:app do
    gem_install "daemons gem_plugin cgi_multipart_eof_fix"
    gem_install_direct "http://rubyforge.org/frs/download.php/18636/fastthread-1.0.gem"
    gem_install_direct "http://rubyforge.org/frs/download.php/16719/mongrel-1.0.1.gem"
  end

  task :setup_web, :roles=>:web do
    run "apt-get -q update"
    apt_install "apache2"
    %w(proxy_http proxy_balancer rewrite).each do |mod|
      run "a2enmod #{mod}"
    end
    buffer = <<-EOT
NameVirtualHost *
<VirtualHost *>
ServerAdmin webmaster@localhost
<Proxy balancer://mongrel_cluster/>
Order allow,deny
Allow from all
#{mongrel_ports.map {|port| "BalancerMember http://127.0.0.1:%d"%port}.join("\n")}
</Proxy>

ErrorLog /var/log/apache2/error.log
LogLevel warn
CustomLog /var/log/apache2/access.log combined
ServerSignature On

DocumentRoot #{deploy_to}/current/public
<Directory #{deploy_to}/current/public>
AllowOverride None
Order allow,deny
Allow from all
</Directory>

RewriteEngine On
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]

</VirtualHost>
  EOT
    put buffer, "/etc/apache2/sites-available/rails", :mode=>644
    run <<-CMD
      a2dissite default;
      a2ensite rails;
      true
    CMD
    run "/etc/init.d/apache2 force-reload"
  end
  task :setup_monit, :roles=>:app do
    apt_install "monit"
    put "startup=1\n", "/etc/default/monit"
    put "include /etc/monit.d/*\n", "/etc/monit/monitrc"
    run "mkdir -p /etc/monit.d"
    put <<-EOT, "/etc/monit.d/monit"
set httpd port 2812 and
  use address localhost
  allow 127.0.0.1
    EOT
    # mongrel
    buffer = ""
    for port in mongrel_ports
      pid_path = "#{deploy_to}/shared/pids/mongrel.#{port}.pid"
      buffer << <<-EOT
# mongrel #{port} 
check process mongrel-#{port} with pidfile #{pid_path}
  start program = "/var/lib/gems/1.8/bin/mongrel_rails start -P #{pid_path} -p #{port} -e production -d -a 127.0.0.1 -c #{deploy_to}/current"
  stop program = "/var/lib/gems/1.8/bin/mongrel_rails stop -P #{pid_path}"

  if failed port #{port} protocol http
    with timeout 10 seconds
    then restart

  group mongrel
      EOT
    end
    put buffer, "/etc/monit.d/mongrel"
    restart_monit
  end
  task :restart_monit, :rolls=>:app do
    run "/etc/init.d/monit restart"
  end
end
